package com.isdoub.testcamera.camera1;

import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.view.TextureView;

import com.isdoub.testcamera.common.CameraOperation;

/**
 * Created by had on 2018/7/15.
 */

public class Camera1Manager implements CameraOperation, TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private TextureView tuvPreview;
    private StateCallback mStateCallback;

    public Camera1Manager(TextureView tuvPreview) {
        this.tuvPreview = tuvPreview;
        if (this.tuvPreview == null)
            throw new IllegalArgumentException("The Textureview used to camera preview can't be null !");
        tuvPreview.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        Camera.CameraInfo cameraInfo = CameraConfigs.getFrontCameraInfo();
        if (cameraInfo == null) {
            if (mStateCallback != null)
                mStateCallback.onError(StateCallback.GET_FRONT_CAMERA_INFO_FAILED, "找不到前置摄像头");
            return;
        }
        mCamera = CameraConfigs.openCamera(cameraInfo.facing);
        if (mCamera==null){
            if (mStateCallback!=null)mStateCallback.onError(StateCallback.OPEN_FRONT_CAMERA_FAILED,"摄像头打开失败");
            return;
        }
        mCamera.autoFocus(null);
        mCamera.startPreview();

    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }

    @Override
    public void autoFocus(boolean enable) {

    }

    @Override
    public void startPreview() {

    }

    @Override
    public void stopPreivew() {

    }

    @Override
    public void takePic() {

    }

    @Override
    public void takeOneShotFrame() {

    }


    public interface LifeCycle {

        int start();

        int resume();

        int pause();

        int stop();

        int release();

    }


    public interface StateCallback {
        int GET_FRONT_CAMERA_INFO_FAILED = 1000;
        int GET_BACK_CAMERA_INFO_FAILED = 1001;
        int OPEN_FRONT_CAMERA_FAILED = 1002;
        int OPEN_BACK_CAMERA_FAILED = 1003;

        int TEXTURE_NOT_AVAILIABLE = 2000;


        void onError(int code, Object msg);


        void onPreviewStarted();

        void onPreviewStoped();

        boolean onFocuseSuccess();

        boolean onPreviewFrame(byte[] data);

        boolean onOnshotFrame(byte[] data);

    }


}
